﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Ext.Net.Utilities;
using Ext.Net;
using MemberMgr.Data.BusinessObjects;
using MemberMgr.Data.ManagerObjects;
using MemberMgr.Website.Common;
using System.Data;

namespace MemberMgr.Website.Member
{
    public partial class MemberCost : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest && !this.IsPostBack)
            {
                
            }
        }

        [DirectMethod(Namespace = "X")]
        public void SearchKeyPress()
        {
            SearchCost();
        }
        

        [DirectMethod]
        protected void btnSearch_Click(object sender, DirectEventArgs e)
        {
            SearchCost();
        }

        private void SearchCost()
        {
            string strMemberCode = string.Empty;

            strMemberCode = this.txtSearchNo.Text;

            //Search Member
            ItMemberManager memMgr = Common.CommonUtil.managerFactory.GettMemberManager();

            IList<tMember> memberlist = memMgr.GetMemberByCardNO(strMemberCode);

            if (memberlist != null && memberlist.Count > 0)
            {
                tMember member = memberlist.FirstOrDefault();

                //设置会员信息
                this.lblMemberInfo.Text = string.Empty;

                this.lblMemberInfo.Html += "&nbsp;&nbsp;&nbsp;&nbsp;<B>会员姓名:</B>" + member.Membername + "&nbsp;&nbsp;";
                this.lblMemberInfo.Html += "<B>会员类型:</B>" + member.Membertype + "&nbsp;&nbsp;";
                this.lblMemberInfo.Html += "<B>会员联系方式:</B>" + member.Membertel + "&nbsp;&nbsp;";
                this.lblMemberInfo.Html += "<B>会员地址:</B>" + member.Memberaddress + "&nbsp;&nbsp;";

                ItConsumptionMasterManager cmMgr = Common.CommonUtil.managerFactory.GettConsumptionMasterManager();

                IList<tConsumptionMaster> list = cmMgr.GetConsumptionByMemberCode(member);

                if (list != null && list.Count > 0)
                {
                    tConsumptionMaster cmMaster = list.FirstOrDefault();

                    //获取详细信息
                    btnMemberID.Text = cmMaster.TMember.Id.ToString();

                    list.OrderBy(p => p.Costtime);
                    DataTable dt = this.GetMemberCostInfoDataTable();

                    foreach (tConsumptionMaster item in list)
                    {
                        dt.Rows.Add(
                            item.Id,
                            item.Sum,
                            item.Costtime,
                            item.Costaddress,
                            item.Costcut,
                            item.TMember.Id,
                            item.TShop.Id,
                            item.TMember.Membername,
                            item.TShop.Shopname,
                            item.Costpoint,
                            item.TMember.Membercard,
                            item.TConsumptionDetails.FirstOrDefault() == null ? (new tProduct()).Productname:item.TConsumptionDetails.FirstOrDefault().TProduct.Productname

                        );
                    }

                    this.storeMemberCostList.DataSource = dt;
                    this.storeMemberCostList.DataBind();

                }
            }
            else
            {
                this.lblMemberInfo.Html = string.Format("&nbsp;&nbsp;&nbsp;&nbsp;<font color='red'>{0}</font>", "没有输入的会员卡号，请确认后重试。");
                this.txtSearchNo.Focus();

                this.gpMemberCostInfo.Items.Clear();
            }
        
        }
        private DataTable GetMemberCostInfoDataTable()
        {
            DataTable dt = new DataTable();

            dt.Columns.AddRange(new DataColumn[] { 
                new DataColumn() { ColumnName="ID",DataType=typeof(uint)},
                new DataColumn() { ColumnName="SUM",DataType=typeof(int)},
                new DataColumn() { ColumnName="COSTTIME",DataType=typeof(DateTime)},
                new DataColumn() { ColumnName="COSTADDRESS",DataType=typeof(string)},
                new DataColumn() { ColumnName="COSTCUT",DataType=typeof(string)},
                new DataColumn() { ColumnName="MEMBERID",DataType=typeof(int)},
                new DataColumn() { ColumnName="SHOPID",DataType=typeof(int)},
                new DataColumn() { ColumnName="MEMBERNAME",DataType=typeof(string)},
                new DataColumn() { ColumnName="SHOPNAME",DataType=typeof(string)},
                new DataColumn() { ColumnName="COSTPOINT",DataType=typeof(float)},
                new DataColumn() { ColumnName="MEMBERCODE",DataType=typeof(string)},
                new DataColumn() { ColumnName="PRODUCTNAME",DataType=typeof(string)}

            });

            return dt;
        }

    }
}
